package com.example.interview.parent_son;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SimplePropertyPreFilter;


import java.util.ArrayList;
import java.util.List;

// 将具有父子关系的数据库表数据转换为树形结构，支持无限层级  https://zhibo.blog.csdn.net/article/details/54616485
public class TreeTestMain {

    public static void main(String[] args) {
        ITreeImpl ITreeImpl = new ITreeImpl(dataListInit());//用接口-实现的方式 避免static调用的出现的问题
        TreeNode treeNode = ITreeImpl.getTreeNode("2");

        SimplePropertyPreFilter filter = new SimplePropertyPreFilter(); // 构造方法里，也可以直接传需要序列化的属性名字
        filter.getExcludes().add("parent");
        filter.getExcludes().add("allChildren");
        String data = JSONObject.toJSONString(treeNode, filter);
        System.out.println(data);
    }

    public static List<ITreeNode> dataListInit(){
        List<ITreeNode> list = new ArrayList<ITreeNode>();

        ITreeNodeImpl ITreeNodeImpl = new ITreeNodeImpl("2", "1", "北京市", 2, "110000", "2");
        list.add(ITreeNodeImpl);
        ITreeNodeImpl = new ITreeNodeImpl("3", "2", "市辖区", 3, "110100", "3");
        list.add(ITreeNodeImpl);
        ITreeNodeImpl = new ITreeNodeImpl("4", "3", "东城区", 4, "110101", "4");
        list.add(ITreeNodeImpl);
        ITreeNodeImpl = new ITreeNodeImpl("5", "3", "东城区", 5, "110102", "4");
        list.add(ITreeNodeImpl);
        ITreeNodeImpl = new ITreeNodeImpl("6", "3", "东城区", 6, "110105", "4");
        list.add(ITreeNodeImpl);
        ITreeNodeImpl = new ITreeNodeImpl("7", "3", "东城区", 7, "110106", "4");
        list.add(ITreeNodeImpl);
        ITreeNodeImpl = new ITreeNodeImpl("8", "3", "东城区", 8, "110107", "4");
        list.add(ITreeNodeImpl);
        ITreeNodeImpl = new ITreeNodeImpl("9", "3", "东城区", 9, "110108", "4");
        list.add(ITreeNodeImpl);
        ITreeNodeImpl = new ITreeNodeImpl("10", "3", "东城区", 10, "110109", "4");
        list.add(ITreeNodeImpl);
        ITreeNodeImpl = new ITreeNodeImpl("11", "3", "东城区", 11, "110111", "4");
        list.add(ITreeNodeImpl);
        ITreeNodeImpl = new ITreeNodeImpl("12", "3", "东城区", 12, "110112", "4");
        list.add(ITreeNodeImpl);
        ITreeNodeImpl = new ITreeNodeImpl("13", "3", "东城区", 13, "110113", "4");
        list.add(ITreeNodeImpl);
        ITreeNodeImpl = new ITreeNodeImpl("14", "3", "东城区", 14, "110114", "4");
        list.add(ITreeNodeImpl);
        ITreeNodeImpl = new ITreeNodeImpl("15", "3", "东城区", 15, "110115", "4");
        list.add(ITreeNodeImpl);
        ITreeNodeImpl = new ITreeNodeImpl("16", "3", "东城区", 16, "110116", "4");
        list.add(ITreeNodeImpl);
        ITreeNodeImpl = new ITreeNodeImpl("17", "3", "东城区", 17, "110117", "4");
        list.add(ITreeNodeImpl);
        ITreeNodeImpl = new ITreeNodeImpl("18", "2", "县", 3, "110200", "3");
        list.add(ITreeNodeImpl);
        ITreeNodeImpl = new ITreeNodeImpl("19", "18", "密云县", 19, "110228", "4");
        list.add(ITreeNodeImpl);
        ITreeNodeImpl = new ITreeNodeImpl("20", "18", "延庆县", 20, "110229", "4");
        list.add(ITreeNodeImpl);
        return list;
    }

}
